<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-suffix=":">
        <el-form-item label="标题" prop="title">
          <el-input
            v-noSpace
            v-model="queryParams.title"
            placeholder="标题"
            class="!w-[192px]"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <!-- <el-form-item label="消息类型" prop="msgType">
          <el-select
            v-model="queryParams.msgType"
            class="!w-[100px]"
            clearable
            placeholder="全部"
            @change="handleQuery()"
          >
            <el-option label="全部" value="" />
            <el-option
              v-for="item in messageTypeOptions"
              :key="item.id"
              :label="item.msgType"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="发送人" prop="publisherName">
          <el-input
            v-noSpace
            v-model="queryParams.publisherName"
            placeholder="发送人"
            class="!w-[192px]"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <!-- <el-form-item label="消息等级" prop="msgLevel">
          <el-select
            v-model="queryParams.msgLevel"
            class="!w-[100px]"
            clearable
            placeholder="全部"
            @change="handleQuery()"
          />
        </el-form-item> -->
        <el-form-item label="是否已读" prop="readStatus">
          <el-select
            v-model="queryParams.readStatus"
            class="!w-[192px]"
            clearable
            placeholder="全部"
            @change="handleQuery()"
          >
            <el-option label="已读" value="1" />
            <el-option label="未读" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery()">搜索</el-button>

          <el-button icon="refresh" @click="handleResetQuery()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-wrapper">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        show-overflow-tooltip
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column label="消息标题" prop="messageTitle" max-width="100" />
        <!-- <el-table-column align="center" label="消息类型">
          <template #default="scope">
            {{ messageTypeOptions.find((item) => item.id === scope.row.messageType)?.msgType }}
          </template>
        </el-table-column> -->
        <el-table-column
          align="center"
          label="消息内容"
          prop="messageContent"
          :tooltip-formatter="withVNode"
        />
        <el-table-column align="center" label="发布人" prop="publisherName" />
        <!-- <el-table-column align="center" label="消息等级">
          <template #default="scope">
            <el-tag v-if="scope.row.messageLevel == 1" type="info">低</el-tag>
            <el-tag v-if="scope.row.messageLevel == 2" type="success">中</el-tag>
            <el-tag v-if="scope.row.messageLevel == 3" type="warning">高</el-tag>
          </template>
        </el-table-column> -->

        <el-table-column align="center" label="是否已读" prop="readStatus" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.readStatus == 0" type="warning">未读</el-tag>
            <el-tag v-if="scope.row.readStatus == 1" type="success">已读</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" min-width="150">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="handleOpenNoticeDetailDialog(scope.row.messageId, scope.row.id)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 消息公告详情 -->
    <NoticeDetail ref="noticeDetailRef" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "MyMessage",
  inheritAttrs: false,
});

import RoleAPI, { RolePageVO } from "@/api/system/role";
import UserAPI, { UserPageVO } from "@/api/system/user";
import MessageConfigAPI, { MessageConfigList } from "@/api/system/messageConfig";
import NoticeDetail from "@/layout/components/NavBar/components/NoticeDetail.vue";
import myMessageAPI, { MyMessagePageQuery, MyMessagePageVO } from "@/api/system/myMessage";

import { onMounted, ref, reactive } from "vue";
import { ElText } from "element-plus";
import { useWebSocketStoreHook } from "@/store";
const useWebSocketStore = useWebSocketStoreHook();

const queryFormRef = ref(ElForm);
const noticeDetailRef = ref();

const loading = ref(false);

const total = ref(0);

const queryParams = reactive<MyMessagePageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const userOptions = ref<UserPageVO[]>([]);
const roleOptions = ref<RolePageVO[]>([]);

// 消息公告表格数据
const pageData = ref<MyMessagePageVO[]>([]);

// 获取用户名
const getUserName = (id: number) => {
  return userOptions.value.find((item) => item.id === id)?.name;
};
// 获取角色名
const getRoleName = (id: number) => {
  return roleOptions.value.find((item) => item.id === id)?.roleName;
};

// 消息等级选项
const messageLevelOptions = ref([
  {
    value: 0,
    label: "低",
  },
  {
    value: 1,
    label: "中",
  },
  {
    value: 2,
    label: "高",
  },
]);
const withVNode = (data: any) => {
  return h(ElText, { type: "primary", href: data.cellValue }, () =>
    h("p", { style: { width: "200px" } }, data.cellValue)
  );
};

//查询消息配置
const messageTypeOptions = ref<MessageConfigList[]>([]);
const getMessageTypeOptions = () => {
  MessageConfigAPI.getMessageConfigList({ pageNum: 1, pageSize: 999 }).then((data) => {
    messageTypeOptions.value = data.list;
  });
};

// 查询消息公告
function handleQuery() {
  loading.value = true;
  myMessageAPI
    .getMessageList(queryParams)
    .then((data) => {
      console.log(data);

      pageData.value = data.list;

      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

// 获取用户和角色选项
const getUserAndRoleOptions = () => {
  UserAPI.getPage({ pageNum: 1, pageSize: 999 }).then((data) => {
    userOptions.value = data.list;
  });
  RoleAPI.getPage({ pageNum: 1, pageSize: 999 }).then((data) => {
    roleOptions.value = data.list;
  });
};

// 打开消息公告详情弹窗
function handleOpenNoticeDetailDialog(messageId: number, id: number) {
  noticeDetailRef.value.openNotice(messageId);
  handleRead(id, messageId);
}

// 标记消息为已读
function handleRead(id: number, messageId: number) {
  console.log(id, messageId);
  useWebSocketStore.readMessages(messageId as number);

  myMessageAPI.readMessage(id).then(() => {
    handleQuery();
  });
}

onMounted(() => {
  handleQuery();
  getMessageTypeOptions();
  getUserAndRoleOptions();
});
</script>
<style>
.editor-wrapper {
  border: 1px solid #dcdfe6;
}
</style>
